using System.Configuration;
namespace AonesoftBase.Biz
{
    using System;
    using System.ComponentModel;
    using System.Data.OleDb;
    using System.Web;

    [DataObject]
    public class IPAccess
    {
        public static readonly string COMMANDTEXT = "Select top 1 city,province from ip_address where ";
        public static readonly string CONNSTRING = (ConfigurationManager.AppSettings["IPConnString"].ToString() + HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) + ";");
        public static readonly string selectcityname = "Select pinyin from city_py Where city=";

        public static string GetClientIP()
        {
            string str = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            switch (str)
            {
                case null:
                case "":
                    str = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    break;
            }
            if ((str != null) && !(str == string.Empty))
            {
                return str;
            }
            return HttpContext.Current.Request.UserHostAddress;
        }

        public static string[] GetCustomCity()
        {
            return GetCustomCity(GetClientIP());
        }

        public static string[] GetCustomCity(string IP)
        {
            string ip = IP;
            string[] strArray = new string[3];
            try
            {
                long num = IPtoNum(ip);
                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = CONNSTRING;
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = COMMANDTEXT + num.ToString() + ">=ip1 and " + num.ToString() + "<=ip2 order by id desc";
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    strArray[0] = reader["city"].ToString();
                    strArray[1] = reader["province"].ToString();
                }
                else
                {
                    strArray[0] = "";
                    strArray[1] = "";
                }
                connection.Close();
                command.CommandText = selectcityname + "'" + strArray[0].ToString() + "'";
                connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    strArray[2] = reader["pinyin"].ToString();
                    return strArray;
                }
                strArray[2] = "可能为内部IP";
            }
            catch (Exception exception)
            {
                strArray[0] = exception.Message.ToString();
                strArray[1] = exception.Source.ToString();
                strArray[2] = exception.ToString();
            }
            return strArray;
        }

        public static long IPtoNum(string Ip)
        {
            string[] strArray = new string[4];
            strArray = Ip.Split(new char[] { '.' });
            return ((((Convert.ToInt64(strArray[0]) * 0x1000000L) + (Convert.ToInt64(strArray[1]) * 0x10000L)) + (Convert.ToInt64(strArray[2]) * 0x100L)) + Convert.ToInt64(strArray[3]));
        }
    }
}

